// LogRecord.java
package no.nordicsemi.android.nrfmesh.database.entity;

import androidx.room.Entity;
import androidx.room.PrimaryKey;
import androidx.room.TypeConverters;
import androidx.room.ColumnInfo;
import no.nordicsemi.android.nrfmesh.database.DateConverter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import com.google.gson.Gson;

@Entity(tableName = "log_records")
@TypeConverters({DateConverter.class})
public class LogRecord {
    @PrimaryKey(autoGenerate = true)
    public int id;

    public Date startTime = new Date(); // 默认当前时间
    public Date endTime = new Date(); // 默认当前时间
    public long duration = 0; // 默认0

    // 用户设置参数
    public int gear = 1; // 默认1
    public int totalTime = 0; // 默认0
    public int totalSegments = 1; // 默认1
    public String configName; // 配置名称

    // 每段参数 (JSON格式存储)
    public String segmentParams; // 不设置默认值，保持为null

    // 实时数据统计 (多种统计值) - 明确指定为非空
    @ColumnInfo(defaultValue = "0.0")
    public float avgVoltage1 = 0f; // 默认0
    @ColumnInfo(defaultValue = "0.0")
    public float maxVoltage1 = 0f; // 默认0
    @ColumnInfo(defaultValue = "0.0")
    public float minVoltage1 = 0f; // 默认0

    @ColumnInfo(defaultValue = "0.0")
    public float avgVoltage2 = 0f; // 默认0
    @ColumnInfo(defaultValue = "0.0")
    public float maxVoltage2 = 0f; // 默认0
    @ColumnInfo(defaultValue = "0.0")
    public float minVoltage2 = 0f; // 默认0

    @ColumnInfo(defaultValue = "0.0")
    public float avgCurrent = 0f; // 默认0
    @ColumnInfo(defaultValue = "0.0")
    public float maxCurrent = 0f; // 默认0
    @ColumnInfo(defaultValue = "0.0")
    public float minCurrent = 0f; // 默认0

    @ColumnInfo(defaultValue = "0.0")
    public float avgImpedance = 0f; // 默认0
    @ColumnInfo(defaultValue = "0.0")
    public float maxImpedance = 0f; // 默认0
    @ColumnInfo(defaultValue = "0.0")
    public float minImpedance = 0f; // 默认0

    // 实时数据变化记录 (JSON格式存储) - 明确指定默认值
    @ColumnInfo(defaultValue = "'[]'")
    public String voltage1History = "[]"; // 默认空数组
    @ColumnInfo(defaultValue = "'[]'")
    public String voltage2History = "[]"; // 默认空数组
    @ColumnInfo(defaultValue = "'[]'")
    public String currentHistory = "[]"; // 默认空数组
    @ColumnInfo(defaultValue = "'[]'")
    public String impedanceHistory = "[]"; // 默认空数组

    // 初始化段参数（根据 totalSegments 自动生成四个字段）
    public void initSegmentParamsIfNeeded() {
        if (segmentParams == null || segmentParams.isEmpty()) {
            Map<Integer, Map<String, Object>> segments = new HashMap<>();
            for (int i = 1; i <= totalSegments; i++) {
                Map<String, Object> params = new HashMap<>();
                params.put("segmentDuration", 0);
                params.put("pulseWidth", 0);
                params.put("waveformType", 0);
                params.put("frequency", 0);
                segments.put(i, params);
            }
            segmentParams = new Gson().toJson(segments);
        }
    }

    // 获取解析后的段参数
    public Map<Integer, Map<String, Object>> getSegmentParamsMap() {
        if (segmentParams == null || segmentParams.isEmpty()) {
            return new HashMap<>();
        }
        try {
            return new Gson().fromJson(segmentParams,
                    new com.google.gson.reflect.TypeToken<Map<Integer, Map<String, Object>>>(){}.getType());
        } catch (Exception e) {
            return new HashMap<>();
        }
    }
}
